import 'package:flutter/material.dart';

class XinDongScrollNotificationContainer extends StatelessWidget {
  final Widget child;
  final int yemian; // 提前几屏加载，默认1页
  final int depth; // 深度，默认为0，第0层就是滚动层
  final Function onLoadMore; // 加载更多方法
  final Function(double currentPixel, double totalPixel)? onScrollStart; // 监听滚动开始
  final Function(double currentPixel, double totalPixel)? onScrollEnd; // 监听滚动结束

  const XinDongScrollNotificationContainer(
      {Key? key,
      required this.onLoadMore,
      this.yemian = 1,
      this.depth = 0,
      required this.child,
      this.onScrollStart,
      this.onScrollEnd})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final viewWidth = MediaQuery.of(context).size.width;
    return NotificationListener<ScrollNotification>(
      onNotification: (notification) {
        if (notification is ScrollStartNotification && notification.depth == depth) {
          final currentPixel = notification.metrics.pixels;
          final totalPixel = notification.metrics.maxScrollExtent;
          // print("开始滚动... $currentPixel $totalPixel");
          if (onScrollStart != null) onScrollStart!(currentPixel, totalPixel);
        } else if (notification is ScrollEndNotification && notification.depth == depth) {
          final currentPixel = notification.metrics.pixels;
          final totalPixel = notification.metrics.maxScrollExtent;
          // print("滚动结束.... $currentPixel $totalPixel");
          if (onScrollEnd != null) onScrollEnd!(currentPixel, totalPixel);
        } else if (notification is ScrollUpdateNotification && notification.depth == depth) {
          ScrollMetrics metrics = notification.metrics;
          if (metrics.extentAfter < viewWidth * yemian) {
            // onLoadMore();
          }
        }
        return false;
      },
      child: child,
    );
  }
}
